package se.webbzon.boltzmann.game.sequence.builder.condition;

import se.webbzon.boltzmann.game.sequence.Sequence;
import se.webbzon.boltzmann.game.sequence.SequenceNode;
import se.webbzon.boltzmann.game.sequence.builder.SequenceBuilder;
import se.webbzon.boltzmann.game.sequence.builder.AbstractSequenceRow;
import se.webbzon.boltzmann.game.sequence.builder.SequenceRowDeflation;
import se.webbzon.boltzmann.game.sequence.condition.*;
import se.webbzon.oschi01.worldarea.WorldArea;

public class ConditionRow extends AbstractSequenceRow {
	
	// The name of the sequence row
	public static String name = "condition";
	
	// The condition of the condition row
	public ConditionEnum condition;
	
	// The left and right hand argument
	public String lh, rh;

	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new condition sequence row. **/
	public ConditionRow(SequenceBuilder builder, ConditionEnum condition) {
		this(builder,condition,null);
	}
	
	/** Creates a new condition sequence row with one argument. **/
	public ConditionRow(SequenceBuilder builder, ConditionEnum condition, String arg) {
		this(builder,condition,arg,null);
	}
	
	/** Creates a new condition sequence row with two arguments. **/
	public ConditionRow(SequenceBuilder builder, ConditionEnum condition, 
						String lh, String rh) {
		super(name,builder);
		this.condition = condition;
		this.lh = lh;
		this.rh = rh;
	
		addLink(builder.rows()-1);
		addLink(builder.rows()-1);
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the condition of this condition row. **/
	public ConditionEnum getCondition() {
		return condition;
	}
	
	/** Sets the condition of this condition row. **/
	public void setCondition(ConditionEnum condition) {
		this.condition = condition;
	}
	
	/** Returns the left hand argument of this condition row. **/
	public String getLeftHandArgument() {
		return lh;
	}
	
	/** Returns the right hand argument of this condition row. **/
	public String getRightHandArgument() {
		return rh;
	}
	
	/** Sets the left hand argument of this condition row. **/
	public void setLeftHandArgument(String lh) {
		this.lh = lh;
	}
	
	/** Sets the right hand argument of this condition row. **/
	public void setRightHandArgument(String rh) {
		this.rh = rh;
	}

	@Override public int maxLinks() {
		return 2;
	}

	@Override public int minLinks() {
		return 2;
	}
	
	@Override public SequenceNode compile(WorldArea world, Sequence sequence) {
		switch (condition) {
		case alwaysTrue: return new AlwaysTrue(sequence);
		case alwaysFalse: return new AlwaysFalse(sequence);
		case random: return new RandomCondition(sequence);
		case boltzmann: return new IsBoltzmann(sequence);
		case rootsie: return new IsRootsie(sequence);
		case equarius: return null;
		case jaws: return null;
		case dexter: return null;
		case my: return null;
		case isEqual: return new IsEqual(sequence,lh,rh);
		case isGreater: return new IsGreater(sequence,lh,rh);
		case isLesser: return new IsLesser(sequence,lh,rh);
		case isGreaterOrEqual: return new IsGreaterOrEqual(sequence,lh,rh);
		case isLesserOrEqual: return new IsLesserOrEqual(sequence,lh,rh);
		case isMatch: return new IsMatch(sequence,lh,rh);
		case isAssigned: return new IsAssigned(sequence,lh);
		case isEnabled: return new IsEnabled(sequence);
		case isCinema: return new IsCinema(sequence);
		case canStore: return new CanStore(sequence);
		case hasGamepad: return new HasGamepad(sequence);
		default: return null;
		}
	}

	@Override public void bind(SequenceNode node, SequenceNode[] nodes) {
		ConditionSequenceNode n = (ConditionSequenceNode) node;
		Integer falseBranch = getLink(1), trueBranch = getLink(0);
		if (falseBranch != null)
			n.setFalseBranch(nodes[falseBranch]);
		if (trueBranch != null)
			n.setTrueBranch(nodes[trueBranch]);
	}

	@Override public SequenceRowDeflation getDeflation() {
		return new ConditionRowDeflation(this);
	}

	@Override public String getText() {
		return condition.name() + "?";
	}	

}
